package com.wzw.springboot.principle.use.security.infrastructure.service;

import com.wzw.springboot.principle.use.security.manage.RbacManager;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 实现security的接口
 *
 * @author Wangzhiwen
 */
@Service
@RequiredArgsConstructor
public class UserDetailServiceImpl implements UserDetailsService {

    private final RbacManager rbacManager;

    /**
     * 账号登录
     *
     * @param account 账号
     * @return 用户登录信息
     * @throws UsernameNotFoundException 账户不存在
     */
    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        // 认证逻辑一般需要放行接口，所以这里不会走到
        // 如果用户不存在，需要抛出 UsernameNotFoundException 后续框架会处理此类型异常。返回为空时，security 框架会抛出 InternalAuthenticationServiceException 异常
        return rbacManager.getSecurityUserByAccount(account);
    }
}
